﻿@inject CardApiService CardApiService
@inject TaskApiService TaskApiService

@if (!cardsSelectList.Any())
{
    <div class="spinner-loading"></div>
}
else
{
    <div class="row">
        <div class="col-lg-12">
            <div class="input-group">
                <select class="form-control" @bind="CardId">
                    @foreach (var prop in cardsSelectList)
                    {
                        <option value="@prop.Value" selected="@(prop.Value==CardId?true:false)">@prop.Text</option>
                    }
                </select>

                <span class="input-group-btn">
                    <button type="button" @onclick="HandleMoveTaskToAnotherCard" class="btn btn-primary">move</button>
                </span>
            </div>
        </div>
    </div>
}





@code {

    [Parameter]
    public string TaskId { get; set; }

    [Parameter]
    public string BoardId { get; set; }

    [Parameter]
    public string CardId { get; set; }


    /// <summary>
    /// cards list to use in DropDownList
    /// </summary>
    IEnumerable<SelectListItem> cardsSelectList = new List<SelectListItem>();



    protected override async Task OnInitializedAsync()
    {
        await GetCardsSelectList();
    }





    /// <summary>
    /// get cards select list to use in DropDownList
    /// </summary>
    private async Task GetCardsSelectList()
    {
        var cardsResult = await CardApiService.GetSelectListItemsAsync(BoardId);
        if (cardsResult.IsSuccess)
            cardsSelectList = cardsResult.Value;
        else
            ToastService.ShowError(cardsResult.Errors.ParseToFragment(), cardsResult.Message);
    }


    /// <summary>
    ///
    /// </summary>
    private async Task HandleMoveTaskToAnotherCard()
    {
        var result = await TaskApiService.MoveTaskToAnotherCardAsync(TaskId, CardId);
        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Task_Moved)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService);
    }


}
